﻿#region LGPL License

/*
OpenSearchNet
Copyright (c) 2014, Brad Hards <bradh@frogmouth.net>, All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library.
*/

#endregion LGPL License

using System;
using System.Linq;

namespace OpenSearch
{
    /// <summary>
    /// Parameter from an OpenSearch Query template
    /// </summary>
    public class TemplateParameter
    {
        private string name;
        private TemplateParameterKind kind;
        private NameSpacedParameter substitutionParameterValue;
        private string parameterValue;

        /// <summary>
        /// Construct TemplateParameter instance from part of a query
        /// </summary>
        /// <param name="queryPart">query part as a string</param>
        /// <param name="namespaces">dictionary of namespace prefixes and associated namespace</param>
        /// <param name="defaultNamespace">default implicit namespace</param>
        /// <remarks>
        /// <para>
        /// The query part is expected to look like key=value. Examples are <c>q={searchTerms}</c> or <c>format=atom</c>.
        /// Search terms values like <c>{geo:bbox?}</c> are handled as optional, per the OpenSearch specification.
        /// </para></remarks>
        public TemplateParameter(string queryPart, System.Collections.Generic.IDictionary<string, string> namespaces, string defaultNamespace)
        {
            if (queryPart == null)
            {
                return;
            }
            string[] queryPartPart = queryPart.Split('=');
            if (queryPartPart.Count() != 2)
            {
                return;
            }
            name = queryPartPart[0];
            string param = queryPartPart[1];
            if (!param.StartsWith("{", StringComparison.OrdinalIgnoreCase))
            {
                kind = TemplateParameterKind.Literal;
                parameterValue = param;
            }
            else if (param.EndsWith("?}", StringComparison.OrdinalIgnoreCase))
            {
                kind = TemplateParameterKind.OptionalSubstitution;
                substitutionParameterValue = new NameSpacedParameter(param.Substring(1, param.Length - 3), namespaces, defaultNamespace);
            }
            else if (param.EndsWith("}", StringComparison.OrdinalIgnoreCase))
            {
                kind = TemplateParameterKind.RequiredSubstitution;
                substitutionParameterValue = new NameSpacedParameter(param.Substring(1, param.Length - 2), namespaces, defaultNamespace);
            }
            else
            {
                return;
            }
        }

        /// <summary>
        /// Parameter name
        /// </summary>
        /// <remarks>
        /// <para>If the template part is <c>q={searchTerms}</c>, then this will give back <c>q</c></para>
        /// </remarks>
        public string Name
        {
            get { return name; }
        }

        /// <summary>
        /// Parameter substitution kind
        /// </summary>
        /// <remarks>
        /// <para>If the template part is <c>q={searchTerms}</c>, then this will give back <c>TemplateParameterKind.RequiredSubstitution</c>.
        /// If the template part is <c>area={geo:bbox?}</c>, then this will give back <c>TemplateParameterKind.OptionalSubstitution</c>.
        /// If the template part is <c>format=atom</c>, then this will give back <c>TemplateParameterKind.Literal</c>.</para>
        /// </remarks>
        public TemplateParameterKind Kind
        {
            get { return kind; }
        }

        /// <summary>
        /// Parameter substitution value
        /// </summary>
        /// <remarks>
        /// <para>If the template part is <c>q={searchTerms}</c>, then this will give back <c>searchTerms</c></para>
        /// </remarks>
        public NameSpacedParameter SubstitutionParameterValue
        {
            get { return substitutionParameterValue; }
        }

        /// <summary>
        /// Parameter value
        /// </summary>
        /// <remarks>
        /// <para>
        /// This property allows get / set on the parameter. Set is parameter substitution for the value the user (or application)
        /// wants to use for this query parameter. This will return an empty string if this is a substitution value. If the
        /// template parameter is a literal (e.g. from something like <c>format=atom</c>), this will return the input literal
        /// (in that example, <c>atom</c>).
        /// </para>
        /// </remarks>
        public string ParameterValue
        {
            get { return parameterValue; }
            set { parameterValue = value; }
        }
    }
}